<!DOCTYPE html>
<html lang="en">

	<head>
		<meta charset="UTF-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<title>Editor</title>
		<script src="src/ace.js" type="text/javascript" charset="utf-8"></script>
		<script src="src/ext-statusbar.js"></script>
		<script type="text/javascript" src="src/ext-language_tools.js"></script>
		<style type="text/css" media="screen">
			body {
				overflow: hidden;
			}
			
			#editor {
				margin: 0;
				position: absolute;
				top: 0;
				bottom: 0;
				left: 0;
				right: 0;
				font-size: 14px;
			}
			
			#statusBar {
				margin: 0;
				padding: 0;
				position: absolute;
				left: 0;
				right: 0;
				bottom: 0;
				height: 20px;
				width: 90%;
				padding-left: 50px;
				padding-right: 30px;
				background-color: rgb(245, 245, 245);
				color: gray;
			}
			
			.ace_status-indicator {
				color: gray;
				position: absolute;
				right: 0;
				border-left: 1px solid;
			}
		</style>
		<script>
			function GetUrlArg(name) {
				var reg = new RegExp("(^|\\?|&)" + name + "=([^&]*)(\\s|&|$)", "i");
				if(reg.test(location.href)) return unescape(RegExp.$2.replace(/\+/g, " "));
				return "";
			};

			var editor;

			function initCode() {
				var height = screen.availHeight - 187;
				parent.document.all(GetUrlArg("frmname")).style.height = height + "px";
				var defaultValue = parent.document.getElementById(GetUrlArg("fdname")).value;
				editor = ace.edit("editor");

				var langTools = ace.require("ace/ext/language_tools");

				editor.getSession().setMode("ace/mode/" + GetUrlArg("mode"));
				if(GetUrlArg("theme") != "") {
					editor.setTheme("ace/theme/" + GetUrlArg("theme"));
				}
				editor.setShowPrintMargin(false);
				editor.setFontSize("14px");
				editor.renderer.setShowPrintMargin(false);
				editor.insert(defaultValue);
				//var StatusBar = ace.require('ace/ext/statusbar').StatusBar;
				//var statusBar = new StatusBar(editor,  document.getElementById('statusBar'));
				editor.commands.addCommands([{
					name: "savedoc",
					bindKey: {
						win: "Ctrl-s",
						mac: "Command-s"
					},
					exec: function(editor) {
						parent.SaveDocument();
					},
					readOnly: true
				}]);

				// 开启提示
				editor.setOptions({
					enableBasicAutocompletion: true,
					enableSnippets: true,
					enableLiveAutocompletion: true
				});

				// 自定义提示数据
				var data = null;
				var url = "" /*json文件url*/
				
				if("java" == GetUrlArg("mode")){
					url = "./linkeyJava.json";
				}
				else if("javascript" == GetUrlArg("mode")) {
					url = "./linkeyJS.json";
				}
				
				if(url != ""){
					url  = url + "?timestamp=" + new Date().getTime();
					
					var request = new XMLHttpRequest();
					request.open("get", url); /*设置请求方法与路径*/
					request.send(null); /*不发送数据到服务器*/
					request.onload = function() { /*XHR对象获取到返回信息后执行*/
						if(request.status == 200) { /*返回状态为200，即为数据获取成功*/
							data = JSON.parse(request.responseText);
						//console.log(request.responseText);
							langTools.addCompleter({
								getCompletions: function(editor, session, pos, prefix, callback) {
									if(prefix.length === 0) {
										return callback(null, []);
									} else {
										return callback(null, data);
									}
								}
							});
						}
					}
				}
				
			}
			

			function getContent() {
				return editor.getValue();
			}
		</script>

	</head>

	<body onload="initCode()">

		<pre id="editor"></pre>

	</body>

</html>